package com.acertificate.dafau;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.ParagraphTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.acertificate.domain.CopyOfNickname;

public class QQGexingQianming {

	HttpPost httppost = new HttpPost("http://dafau.sinaapp.com/share/mood");

	public static final String URL_PREFIX = "http://www.gexing.com/qqqianming/new/";
	public static final String SUFFIX = "";

	public static int maxProcess = 50;
	public static int total = 3372;
	public static int threadCount;

	{
		threadCount = total / maxProcess + (total % maxProcess == 0 ? 0 : 1);
		System.out.println(threadCount);
	}

	public static void main(String[] args) throws ParserException, MalformedURLException, Exception {
		QQGexingQianming wanmv = new QQGexingQianming();
		for (int i = 1; i <= threadCount; i++) {
			RelolThread thread = wanmv.new RelolThread((i - 1) * maxProcess + 1, (i * maxProcess > total ? total : i
					* maxProcess));
			thread.start();
		}
	}

	class RelolThread extends Thread {

		public int start;
		public int end;

		public RelolThread() {
		}

		public RelolThread(int start, int end) {
			this.start = start;
			this.end = end;
		}

		public void run() {
			for (int i = start; i < end; i++) {
				getManifest(i, URL_PREFIX + i + SUFFIX);
			}
		}

		public List<String> getManifest(int x, String url) {
			System.out.println(url);
			HttpClient httpclient = new DefaultHttpClient();
			List<String> results = new ArrayList<String>();
			try {
				URLConnection urlConnection = new URL(url).openConnection();
				String content1 = IOUtils.toString(urlConnection.getInputStream(),"utf-8");
				Parser contentParser = new Parser();
				contentParser.setInputHTML(content1);
				AndFilter andFilter = new AndFilter(new NodeClassFilter(ParagraphTag.class), new HasAttributeFilter(
						"class", "postContent"));
				NodeList xx = contentParser.extractAllNodesThatMatch(andFilter);
				for (int i = 0; i < xx.size(); i++) {
					ParagraphTag node = (ParagraphTag) xx.elementAt(i);

					String txt1 = node.toPlainTextString();
					txt1 = txt1.trim().replace(" ", "");
					txt1 = txt1.replace("\\", "\\ ");
					txt1 = txt1.replace("\n", "");
					txt1 = txt1.replace("\n\r", "");
					// txt = txt.replace("\"", "\"");
					txt1 = txt1.replace("'", "\\'");
					
					System.out.println(txt1);
					MultipartEntity reqEntity = new MultipartEntity();

					StringBody field1 = new StringBody(txt1.replace("&nbsp;", "").trim(), Charset.forName("UTF-8"));

					reqEntity.addPart("field1", field1);
					httppost.setEntity(reqEntity);

					HttpResponse response = httpclient.execute(httppost);
					HttpEntity resEntity = response.getEntity();

					if (resEntity != null) {
						System.out.println(IOUtils.toString(resEntity.getContent()));
					}
					EntityUtils.consume(resEntity);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}

			return results;
		}
	}

}
